﻿var express = require('express');
var http = require('http');
var path = require('path');
var config = require('/node/chat/config');
var log = require('/node/chat/lib/log')(module);
var mongoose = require('/node/chat/lib/mongoose');
var HttpError = require('/node/chat/error').HttpError;

var app = express();

app.engine('ejs', require('ejs-locals'));
app.set('views', path.join(__dirname, '/template'));
app.set('view engine', 'ejs');

app.use(express.favicon());

if (app.get('env') == 'development'){
  app.use(express.logger('dev'));
} else { 
  app.use(express.logger('default'));
}

app.use(express.bodyParser());

app.use(express.cookieParser());

var MongoStore = require('connect-mongo')(express);

app.use(express.session({
  secret: config.get('session:secret'),
  key: config.get('session:key'),
  cookie: config.get('session:cookie'),
  store: new MongoStore({mongoose_connection: mongoose.connection})
}));
/*
app.use(function(req, res, next) {
  req.session.numberOfVisits = req.session.numberOfVisits + 1 || 1;
  res.send("Visits: " + req.session.numberOfVisits);
  });
*/
app.use(require('/node/chat/middleware/sendHttpError'));
app.use(require('/node/chat/middleware/loadUser'));

app.use(app.router);

require('/node/chat/routes')(app);

app.use(express.static(path.join(__dirname, 'public')));

app.use (function(err, req, res, next) {
  if (typeof err == 'number') {
    err = new HttpError(err);
  }
  if (err instanceof HttpError) {
    res.sendHttpError(err);
  } else {
    if (app.get('env') == 'development') {
	  express.errorHandler()(err, req, res, next);
	} else {
	  log.error(err);
	  err = new HttpError(500);
	  res.sendHttpError(err);
	}
  } 
});

var server = http.createServer(app);
server.listen(config.get('port'), function() {
  //console.log('Express server listening on port ' + config.get('port'));
  log.info('Express server listening on port ' + config.get('port'));
});

require('./socket')(server);